﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class ThayDoiLuongBOCollection : Csla.BusinessListBase<ThayDoiLuongBOCollection, ThayDoiLuongBO>
    {
        public int SttIndex = 0;

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            ThayDoiLuongBO _ThayDoiLuongBO = ThayDoiLuongBO.NewChildThayDoiLuongBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_ThayDoiLuongBO));
            _ThayDoiLuongBO.STT0 = ++SttIndex;
            Add(_ThayDoiLuongBO);
            return _ThayDoiLuongBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private ThayDoiLuongBOCollection()
        {
        }

        public static ThayDoiLuongBOCollection NewThayDoiLuongBOCollection()
        {
            return new ThayDoiLuongBOCollection();
        }

        public static ThayDoiLuongBO NewThayDoiLuongBO()
        {
            return ThayDoiLuongBO.NewChildThayDoiLuongBO();
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection()
        {
            SPParam spParam = new SPParam("ThayDoiLuongID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("ThayDoiLuongID", "ASC", "");
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection(string where)
        {
            SPParam spParam = new SPParam("ThayDoiLuongID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("ThayDoiLuongID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThayDoiLuongID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThayDoiLuongID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThayDoiLuongID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }

        public static ThayDoiLuongBOCollection GetThayDoiLuongBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThayDoiLuongID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThayDoiLuongBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThayDoiLuongBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    ThayDoiLuongBO objThayDoiLuongBO = ThayDoiLuongBO.GetChildThayDoiLuongBO(dr);
                    this.Add(objThayDoiLuongBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            ThayDoiLuongBO child = null;
            int length = this.Count;
            bool del = false;
            for (int i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.DELE);

                if (child.IsNew && !del)
                    child.Insert();
                else
                    child.Update();

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public ThayDoiLuongBO GetThayDoiLuongBO(int _ThayDoiLuongID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].ThayDoiLuongID == _ThayDoiLuongID)
                    return this[i];

            return null;
        }

        public void Remove(int ThayDoiLuongID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].ThayDoiLuongID == ThayDoiLuongID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}
